package com.oos.zone.wechat.cp.service.handler;

import lombok.SneakyThrows;
import lombok.extern.log4j.Log4j2;
import me.chanjar.weixin.cp.api.WxCpExternalContactService;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.external.WxCpWelcomeMsg;
import me.chanjar.weixin.cp.bean.external.contact.ExternalContact;
import me.chanjar.weixin.cp.bean.external.contact.FollowedUser;
import me.chanjar.weixin.cp.bean.external.contact.WxCpExternalContactInfo;
import me.chanjar.weixin.cp.bean.external.msg.Text;
import me.chanjar.weixin.cp.bean.message.WxCpXmlOutMessage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 功能描述：外部联系人事件处理器
 *
 * @author 拓金辉
 * @version 1.0
 * @date 2021-07-29 23:08:49
 */
@Log4j2
@Service
public class WxCpChangeExternalContactMessageHandler extends AbstractWxCpMessageHandler {

    @Resource
    private WxCpService wxCpService;

    @Override
    public WxCpXmlOutMessage doHandle() {
        switch (wxMessage.getChangeType()) {
            case "add_external_contact":
                return addExternalContact();
            case "edit_external_contact":
                return editExternalContact();
            case "add_half_external_contact":
                return addHalfExternalContact();
            case "del_external_contact":
                return delExternalContact();
            case "del_follow_user":
                return delFollowUser();
            case "transfer_fail":
                return transferFail();
            default:
                log.error("\n\n【事件类型未处理】\n\n {}", wxMessage.toString());
                return defaultTextMessage;
        }
    }

    @SneakyThrows
    public WxCpXmlOutMessage addExternalContact() {
        CompletableFuture.runAsync(() -> log.info("添加企业客户事件"), executor);
        String externalUserId = wxMessage.getExternalUserId();
        String welcomeCode = wxMessage.getWelcomeCode();
        String state = wxMessage.getState();

        final WxCpExternalContactService wxCpExternalContactService = wxCpService.getExternalContactService();
        final WxCpExternalContactInfo wxCpExternalContactInfo = wxCpExternalContactService.getContactDetail(externalUserId, null);
        final ExternalContact externalContact = wxCpExternalContactInfo.getExternalContact();
        CompletableFuture.runAsync(() -> this.recursionFollowedUser(wxCpExternalContactInfo), executor);

        // TODO 此处需根据配置动态发送欢迎语
        if (StringUtils.isNoneBlank(externalUserId, welcomeCode)) {
            Text text = new Text();
            text.setContent("你好啊," + externalContact.getName());
            wxCpExternalContactService.sendWelcomeMsg(new WxCpWelcomeMsg(welcomeCode, text, null));
        }
        return defaultTextMessage;
    }

    @SneakyThrows
    public void recursionFollowedUser(WxCpExternalContactInfo wxCpExternalContactInfo) {
        final WxCpExternalContactService externalContactService = wxCpService.getExternalContactService();

        final ExternalContact externalContact = wxCpExternalContactInfo.getExternalContact();
        final List<FollowedUser> followedUsers = wxCpExternalContactInfo.getFollowedUsers();
        final String nextCursor = wxCpExternalContactInfo.getNextCursor();

        while (StringUtils.isNotBlank(nextCursor)) {
            this.recursionFollowedUser(externalContactService.getContactDetail(externalContact.getExternalUserId(), nextCursor));
        }
    }

    public WxCpXmlOutMessage editExternalContact() {
        CompletableFuture.runAsync(() -> log.info("编辑企业客户事件"), executor);
        return defaultTextMessage;
    }

    public WxCpXmlOutMessage addHalfExternalContact() {
        CompletableFuture.runAsync(() -> log.info("外部联系人免验证添加成员事件"), executor);
        return defaultTextMessage;
    }

    public WxCpXmlOutMessage delExternalContact() {
        CompletableFuture.runAsync(() -> log.info("删除企业客户事件"), executor);
        return defaultTextMessage;
    }

    public WxCpXmlOutMessage delFollowUser() {
        CompletableFuture.runAsync(() -> log.info("删除跟进成员事件"), executor);
        return defaultTextMessage;
    }

    public WxCpXmlOutMessage transferFail() {
        CompletableFuture.runAsync(() -> log.info("客户接替失败事件"), executor);
        return defaultTextMessage;
    }

}
